export function ListToDict(list, keyName) {
  const dict = {}
  list.map(it => {
    dict[it[keyName]] = it
  })
  return dict // 对数据进行解密
}
export function DeepClone(source) {
  const targetObj = source instanceof Array ? [] : {}; // 判断复制的目标是数组还是对象
  for (const keys in source) { // 遍历目标
    if (source.hasOwnProperty(keys)) {
      if (source[keys] && typeof source[keys] === 'object') { // 如果值是对象，就递归一下
        targetObj[keys] = source[keys].constructor === Array ? [] : {};
        targetObj[keys] = DeepClone(source[keys]);
      } else { // 如果不是，就直接赋值
        targetObj[keys] = source[keys];
      }
    }
  }
  return targetObj;
}


//泡泡特效
let BubbleAnimate = true
export function ClearBubble() {
  BubbleAnimate = false
}

export function SetBubble(id, number) {
  BubbleAnimate = true
  const WIDTH = document.documentElement.clientWidth
  const HEIGHT = document.documentElement.clientHeight
  const ctx = document.getElementById(id)
  const content = ctx.getContext('2d')
  const round = []
  const initRoundPopulation = number
  ctx.width = WIDTH
  ctx.height = HEIGHT
  function Bubble(index, x, y) {
    this.index = index
    this.x = x
    this.y = y
    this.r = Math.random() * 8 + 1
    this.speed = (10 - this.r) / 20
    var alpha = (Math.floor(Math.random() * 10) + 1) / 10 / 2
    this.color = 'rgba(255,255,255,' + alpha + ')'
  }
  Bubble.prototype.draw = function() {
    content.fillStyle = this.color
    content.shadowBlur = this.r * 2
    content.beginPath()
    content.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false)
    content.closePath()
    content.fill()
  }

  Bubble.prototype.move = function() {
    // this.y -= 0.30//  上升移动速度
    this.y -= this.speed
    if (this.y <= -10) {
      this.y = HEIGHT + 10
    }
    this.draw()
  }
  for (var i = 0; i < initRoundPopulation; i++) {
    round[i] = new Bubble(i, Math.random() * WIDTH, Math.random() * HEIGHT)
    round[i].draw()
  }
  content.clearRect(0, 0, WIDTH, HEIGHT)
  for (const i in round) {
    round[i].move()
  }
  function animate() {
    content.clearRect(0, 0, WIDTH, HEIGHT)
    for (var i in round) {
      round[i].move()
    }
    const animateId = requestAnimationFrame(animate)
    if (!BubbleAnimate) {
      cancelAnimationFrame(animateId)
    }
  }
  animate()
}

